package com.huaxin.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.request.EquipmentCategoryCreateReqDTO;
import com.huaxin.device.dto.request.EquipmentCategoryPageListReqDTO;
import com.huaxin.device.dto.request.EquipmentCategoryUpdateReqDTO;
import com.huaxin.device.models.EquipmentCategory;
import com.huaxin.device.service.EquipmentCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "电力设备分类")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/device/equipmentCategory")
public class EquipmentCategoryController {

    private final EquipmentCategoryService service;

    @Operation(summary = "创建设备分类", description = "创建新的设备分类")
    @PostMapping("/create")
    public ResponseResult<EquipmentCategory> create(@RequestBody @Valid EquipmentCategoryCreateReqDTO in) {
        return service.create(in);
    }

    @Operation(summary = "获取设备分类", description = "根据ID获取设备分类详情")
    @GetMapping("/get")
    public ResponseResult<EquipmentCategory> get(
            @Parameter(description = "分类ID", example = "c123e456-789a-bcde-f012-3456789abcde")
            @RequestParam(value = "id", required = true) String id) {
        EquipmentCategory entity = service.getByCategoryId(id);
        return ResponseResult.ok(entity);
    }

    @Operation(summary = "更新设备分类", description = "更新设备分类信息")
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestBody @Valid EquipmentCategoryUpdateReqDTO in) {
        return service.update(in);
    }

    @Operation(summary = "删除设备分类", description = "删除指定ID的设备分类（需为叶子节点且无子项）")
    @DeleteMapping("/{id}/delete")
    public ResponseResult<String> delete(@PathVariable String id) {
        return service.delete(id);
    }

    @Operation(summary = "分页查询设备分类", description = "支持按名称、编码、是否启用等条件分页查询")
    @PostMapping("/page")
    public ResponseResult<IPage<EquipmentCategory>> pageQuery(@RequestBody @Valid EquipmentCategoryPageListReqDTO in) {
        return service.pageQuery(in);
    }

    @Operation(summary = "获取所有分类树", description = "获取完整的设备分类树形结构")
    @GetMapping("/tree")
    public ResponseResult<List<EquipmentCategory>> getTree() {
        return ResponseResult.ok(service.getCategoryTree());
    }
}